import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { CacheService } from 'src/cache/cache.service';
// import { sha256 } from 'crypto-js'
// import sha256 from 'crypto-js/sha256'
let SHA256 = require("crypto-js/sha256")
import { sha256 } from '../utils/hash'

// dlsq-push
// export const GETUI = {
//   AppID: '8pd9glEL8IARCAbCJWI9d6',
//   AppKey: 'dvWef2RZIH7SKvZARpQBl4',
//   AppSecret: 'WIPQCknT6g5Qn6eGW7vZe7',
//   MasterSecret:'vkwwfhdExN6KvYnrM11S25',
//   BaseUrl: `https://restapi.getui.com/v2/8pd9glEL8IARCAbCJWI9d6`
// }

//81hbz-city
export const GETUI = {
  AppID: 'JZN0XkTg2BAQplV8Iv5Bp7',
  AppKey: '8fCe6eYEXWAB06ycSHykV4',
  AppSecret: 'xi14Rx23flANcSFd4vOaWA',
  MasterSecret:'t2T47DvyIh9tkxdf4Qz648',
  BaseUrl: `https://restapi.getui.com/v2/JZN0XkTg2BAQplV8Iv5Bp7`,
  pkgNmae: 'com.bz.yn.city',
}

@Injectable()
export class PushService {

  constructor(
    private readonly cacheService: CacheService
  ) {}

  async getGetuiToken() {
    let timestamp = new Date().valueOf()
    // let sign = SHA256(`${GETUI.AppKey}${timestamp}${GETUI.MasterSecret}`).toString()
    let sign = sha256(`${GETUI.AppKey}${timestamp}${GETUI.MasterSecret}`, 'utf-8', 'hex')
    let d = {
      sign,
      timestamp,
      appkey: GETUI.AppKey
    }
    let {data} = await axios.post(`${GETUI.BaseUrl}/auth`, d)
    this.cacheService.set("GETUI_TOKEN", data.data.token, 2 * 3600)
    return data
      // .then(r => {
      //   return r.data
      // })
      // .catch(e => {
      //   console.log(e)
      //   return e
      // })
  }

  async pushToSingle(cid: string) {
    let token = await this.cacheService.get('GETUI_TOKEN')
    if (!token) {
      await this.getGetuiToken()
      token = await this.cacheService.get('GETUI_TOKEN')
    }

    let intent = `intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=${GETUI.pkgNmae}/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=测试标题;S.content=测试内容;S.payload=测试推送payload;end`
    let d = {
      request_id: new Date().valueOf(),
      settings: {
        ttl: 1 * 24 * 3600 * 1000,
        strategy: {
          default: 4,
        }
      },
      audience: {
        cid: [ cid ]
      },
      push_message: {
        transmission: JSON.stringify({
          title: '您有新的订单',
          content: 'from-个推(node)',
          payload: '11111'
        })
      },
      push_channel: {
        android: {
          ups: {
            notification:{    
              title:"强哥",    
              body:"from-厂商(node)",    
              click_type:"intent",    
              intent   
            }    
          }
        }
      }
    }
    let {data} = await axios.post(`${GETUI.BaseUrl}/push/single/cid`, d, {
      headers: { token }
    })
    return data
  }

  async pushToAll() {
    let token = await this.cacheService.get('GETUI_TOKEN')
    if (!token) {
      await this.getGetuiToken()
      token = await this.cacheService.get('GETUI_TOKEN')
    }
    let d = {
      request_id: new Date().valueOf(),
      settings: {
        ttl: 1 * 24 * 3600 * 1000,
        strategy: {
          default: 4,
        }
      },
      audience: 'all',
      push_message: {
        transmission: JSON.stringify({
          title: '恭喜发财~~~',
          content: 'from-个推(node)',
          payload: '11111'
        })
      },
      push_channel: {
        android: {
          ups: {
            notification:{    
              title:"恭喜发财",    
              body:"from-厂商(node)",    
              click_type:"intent",    
              intent:"intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=uni.UNI3803C1F/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=测试标题1;S.content=测试内容1;S.payload=test;end"    
            }    
          }
        }
      }
    }
    let {data} = await axios.post(`${GETUI.BaseUrl}/push/all`, d, {
      headers: { token }
    })
    return data
  }

}
